package com.morgan.matrix;

import java.util.ArrayList;
import java.util.List;


/**
 * compact it and store it. 
 * @author morgan.liao
 *
 */

public class SparseMatrix {
    
    
    private int rows;
    private int columns;
    
    private List<Triple> terms;
    
    public SparseMatrix(int rows, int columns, List<Triple> t) {
        
        this.rows = rows;
        this.columns = columns;
        this.terms = t;
        
    }
    
    public void output() {
        System.out.println("This sparse matrix: total rows" + 
                rows + "total columns" + columns);
        
        for (int i =0 ; i< terms.size();i++ ) {
            System.out.println(terms.get(i));
        }
    }
    
    public SparseMatrix transponse() {
        
        SparseMatrix sparseMatrix = new SparseMatrix(rows, columns, new ArrayList<Triple>());
        
        if(terms.size() ==0) {
            return sparseMatrix;
        }
        
        for(int k= 1;k <columns; k++) {
            for(int j=0;j<terms.size(); j++) {
                Triple x = (Triple) terms.get(j);
                if(x.column == k) {
                    sparseMatrix.terms.add(new Triple(x.row, k, x.value));
                }
            }
        }
        
        return sparseMatrix;
    }

}



